package com.haina.vote.utils;
import com.haina.vote.entity.KqFwModel;
import com.haina.vote.mapper.KqFwMapper;
import com.haina.vote.service.IKqFwService;
import com.haina.vote.service.impl.KqFwService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Random;
public class RandomCodeUtils {
    public static String RandomCode() {

        String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb=new StringBuilder(6);
        for(int i=0;i<6;i++)
        {
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        System.out.println(sb.toString());
        return sb.toString();
    }

    public static String number(KqFwModel kqFwModel){
        //使用默认时区和语言环境获得一个日历
        Calendar rightNow=Calendar.getInstance();
        //获取当前年份
        Integer year= rightNow.get(Calendar.YEAR);
        String MaxNum="";
        if (kqFwModel==null){
            MaxNum=year+"-0000";
        }else {
            MaxNum=kqFwModel.getNumber();
        }
        String num=MaxNum.substring(MaxNum.lastIndexOf("-")+1);
        int newNumber = Integer.parseInt(num)+1;
        String newNumberStr = String.format(year+"-"+"%04d",newNumber);
        return newNumberStr;
    }
    //生成编号的新规则
    public static String findNewTaskNo(KqFwModel kqFwModel){
        //使用默认时区和语言环境获得一个日历
        Calendar rightNow=Calendar.getInstance();
        //获取当前年份
        Integer year= rightNow.get(Calendar.YEAR);
        String initTaskNo=year+"0001";
        //获取目前任务单号最大的任务
        String maxTaskNo = kqFwModel.getNumber();
        if (maxTaskNo!=null){
            //获取这个订单号的年月日信息
            String yearInfo=maxTaskNo.substring(0,4);
            //判断这个任务单是当年生成的
            if (year.equals(yearInfo)){
                String taskNo =maxTaskNo.substring(4);
                initTaskNo = year+String.format("%04d",Integer.parseInt(taskNo)+1);
            }
        }
        return initTaskNo;
    }
}
